package com.lisx.module_time_block.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.lisx.module_time_block.R;
import com.lisx.module_time_block.bean.NewBlockColorBean;
import com.lisx.module_time_block.databinding.ItemNewBlockColorBinding;

import java.util.List;

public class NewBlockColorAdapter extends RecyclerView.Adapter<NewBlockColorAdapter.MyViewHolder> {

    List<NewBlockColorBean> colorData;
    Context context;

    public NewBlockColorAdapter(Context context, List<NewBlockColorBean> colorData) {
        this.context = context;
        this.colorData = colorData;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNewBlockColorBinding binding = ItemNewBlockColorBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.binding.container.setSelected(colorData.get(position).isSelect);
        holder.binding.v.setBackgroundColor(Color.parseColor(colorData.get(position).color));
        holder.binding.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (NewBlockColorBean colorBean : colorData) {
                    colorBean.isSelect = false;
                }
                colorData.get(position).isSelect = true;
                notifyDataSetChanged();
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return colorData.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemNewBlockColorBinding binding;

        public MyViewHolder(@NonNull ItemNewBlockColorBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
